package model;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import controller.LanceController;

import factory.HibernateUtil;


public class LanceDao {
	public static List<Lance> buscarTodos() {
        List<Lance> lances = new ArrayList<Lance>();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            lances = session.createQuery("from Lance").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return lances;
    }
 
 public static void salvar(Lance lance){
	 Transaction trns = null;
	 Session session = HibernateUtil.getSession();
	 try {
		 trns = session.beginTransaction();
		 session.save(lance);
		 session.getTransaction().commit();
	 } catch (RuntimeException e) {
		 if (trns != null) {
			 trns.rollback();
		 }
		 e.printStackTrace();
	 } finally {
		 session.flush();
		 session.close();
	 }
 }
 
 public static Lance buscarPorId(String id) {
        Lance lance = null;
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        Query query = session.createQuery("from Lance where idLance = "+ id);
        try {
            trns = session.beginTransaction();
            lance = (Lance) query.uniqueResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return lance;
    }
 
 public static void alterarLance(Lance lance) {
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            session.update(lance);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
 }
 
 public static void excluirLance(Lance lance) {
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        try {
            trns = session.beginTransaction();
            session.delete(lance);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
//	public static Lance buscarUltimoLance(Edital edital) {
//		int idLance;
//		Lance lance = new Lance();
//        Transaction trns = null;
//        Session session = HibernateUtil.getSession();
//        Query query = session.createSQLQuery("select max(idLance) from lances where fk_idEdital = " + edital.getIdEdital());
//        try {
//            trns = session.beginTransaction();
//            idLance = Integer.parseInt(query.uniqueResult()+"");
//            System.out.println(idLance);
//            lance = (Lance) buscarPorId(idLance+"");
//        } catch (RuntimeException e) {
//            e.printStackTrace();
//        } finally {
//            session.flush();
//            session.close();
//        }
//        return lance;
//	}
	
	public static Lance buscarUltimoLance(Edital edital) {
		Lance lance = new Lance();
        Transaction trns = null;
        Session session = HibernateUtil.getSession();
        Query query = session.createQuery(
        		"from Lance l where l.idLance in (select max(l2.idLance) from Lance l2 where l2.edital = :edital)")
        		.setParameter("edital", edital);
        
        try {
            trns = session.beginTransaction();
            lance = (Lance) query.uniqueResult(); 
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return lance;
	}
}
